package com.cenwangkeji.resident.ui.activity;

import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.cenwangkeji.resident.R;
import com.cenwangkeji.resident.model.Constant;
import com.cenwangkeji.resident.model.OkHttpUtils;
import com.cenwangkeji.resident.model.bean.SuccessBean;
import com.lws.basic.base.BaseActivity;
import com.lws.basic.persenter.ObserverOnNext;
import com.lws.basic.utils.common.MyToast;
import com.lws.basic.utils.common.RxTime;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class ForgetPasswordActivity extends BaseActivity {


    @BindView(R.id.et_phone)
    EditText mEtPhone;
    @BindView(R.id.et_code)
    EditText mEtCode;
    @BindView(R.id.et_new_password)
    EditText mEtNewPassword;
    @BindView(R.id.et_confirm_password)
    EditText mEtConfirmPassword;
    @BindView(R.id.btn_send)
    Button mBtnSend;

    @Override
    public int setLayoutId() {
        return R.layout.activity_forget_password;
    }

    @Override
    public void initData() {
        super.initData();
        ButterKnife.bind(this);
    }

    @OnClick({R.id.btn, R.id.btn_send})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn:
                confirm();
                break;
            case R.id.btn_send:
                send();
                break;
        }
    }

    private void confirm() {

        String phone = mEtPhone.getText().toString().trim();
        String code = mEtCode.getText().toString().trim();
        String pass = mEtNewPassword.getText().toString().trim();
        String confirm = mEtConfirmPassword.getText().toString().trim();
        if (TextUtils.isEmpty(phone)){
            MyToast.show("手机号不能为空");
        } else if (TextUtils.isEmpty(code)){
            MyToast.show("验证码不能为空");
        } else if(TextUtils.isEmpty(pass)&&TextUtils.isEmpty(confirm)){
            MyToast.show("密码不能为空");
        } else if (!pass.equals(confirm)){
            MyToast.show("两次密码不一致");
        }else { showLoading();
            Map<Object,Object> map = new HashMap<>();
            map.put("brdh",phone);
            map.put("sms", Integer.valueOf(code));
            map.put("pas", pass);
            OkHttpUtils.post(Constant.forget_password, map,SuccessBean.class).subscribe(new ObserverOnNext<SuccessBean>() {
                @Override
                public void onNext(SuccessBean bean) {
                    if (bean.getCode()==0){
                        MyToast.show("修改成功");
                        finish();
                    }else {
                        MyToast.show(bean.getMsg());
                    }
                    dismissLoading();
                }
            });
        }
    }

    private void send() {
        String phone = mEtPhone.getText().toString().trim();
        if (TextUtils.isEmpty(phone)){
            MyToast.show("手机号不能为空");
        }else {
            Map<Object,Object> map = new HashMap<>();
            map.put("brdh",phone);
            OkHttpUtils.post(Constant.forget_password_sms, map, SuccessBean.class).subscribe(new ObserverOnNext<SuccessBean>() {
                @Override
                public void onNext(SuccessBean bean) {
                    if (bean.getCode()==0){
                        mBtnSend.setClickable(false);
                        mBtnSend.setText("59s");
                        final RxTime time = new RxTime();
                        time.interval(1000, new RxTime.RxAction() {
                            @Override
                            public void action(long number) {
                                mBtnSend.setText(58-number+"s");
                                if (number==58){
                                    mBtnSend.setClickable(true);
                                    mBtnSend.setText("发送短信");
                                    time.cancel();
                                }

                            }
                        });
                        MyToast.show("发送成功");
                    }else {
                        MyToast.show(bean.getMsg());
                    }
                }
            })
            ;
        }
    }


}
